package jangdolbaeng2.seller.action;

import jangdolbaeng2.seller.dao.BuilderDao;
import jangdolbaeng2.seller.dao.ProductManagerDAO;
import jangdolbaeng2.seller.dao.SellerDao;
import jangdolbaeng2.tools.Base64Converter;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.ShopVO;
import jangdolbaeng2.vo.Shop_blogVO;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/*
 *작성자 : 진승민
 *기능 : 상점 블로그 제작 기능 액션 관리
 */

@SuppressWarnings("serial")
public class BuilderAction extends ActionSupport implements SessionAware{
	private SessionMap<String, Object> sessionMap; // 세션 map
	
	private List<ProductVO> imageList;				// 상점 상품 이미지 목록
	private Shop_blogVO shop_blog;					// 상점 블로그 정보
	private ShopVO shop;									// 상점 정보
	private int shop_seq;									// 상점 시퀀스
	private ArrayList<String> skinList;				// 스킨 목록
	private ArrayList<String> fontList;				// 폰트 목록
	private ArrayList<String> colorList;				// 컬러 목록
	private String skin;										// 선택 스킨

	private static final String UploadPath = "C:/test/";
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private List<File> uploads = new ArrayList<>();
	private List<String> uploadsContentType = new ArrayList<>();
	private List<String> uploadsFileName = new ArrayList<>();
	
	// 블로그 세팅 변수들	
	private String blog_font;
	private String blog_bgcolor;
	private String blog_style;
	private int blog_slider;
	private int blog_map;

	File saveFile;
	
	// 블로그 builder
	public String blogBuilder(){		
		shop_blog=new SellerDao().blogSetup((int) sessionMap.get("shop_seq")); // 개인 설정 정보를 DB에서 불러옴		
		shop=new SellerDao().getShop((int) sessionMap.get("shop_seq")); // 개인 설정 정보를 DB에서 불러옴
		ProductManagerDAO pDao= new ProductManagerDAO();
		imageList =pDao.list((int) sessionMap.get("shop_seq"));
		BuilderDao dao = new BuilderDao();
		skinList = (ArrayList<String>) dao.skinList();
		fontList = (ArrayList<String>) dao.fontList();
		colorList = (ArrayList<String>) dao.colorList();
		if(skin !=null)	shop_blog.setBlog_style(skin); // 스킨 변경 요청
			sessionMap.put("current_shop_seq", (int) sessionMap.get("shop_seq"));		
		return SUCCESS;
	}


	// 블로그 세팅 정보를 생성하여 DB에 저장
	public String blogSetting() {	
		BuilderDao dao = new BuilderDao();
		//System.out.println(blog_font+blog_bgcolor+blog_style+blog_slider);
		//System.out.println(shop_blog);
		if (upload != null)	shop_blog.setBlog_photo(fileUpload());
		shop_blog.setShop_seq((int) sessionMap.get("shop_seq"));
		dao.blogSetting(shop_blog);
		return blogBuilder();
	}

	// 이미지 파일 업로드 메소드
	public String fileUpload() {

		// 이미지를 텍스트로 변환 시키는 툴
		Base64Converter b64c = new Base64Converter();
		// Null Check
		if (upload != null && upload.exists()) {
			saveFile = new File(UploadPath + uploadFileName);
		}
		try {
			FileUtils.copyFile(upload, saveFile);
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		
		return b64c.imageMaker(saveFile.getAbsolutePath());
	}

	// 멀티 파일 업로드 메소드
	public String multiUpload() {

		// 이미지를 텍스트로 변환 시키는 툴
		Base64Converter b64c = new Base64Converter();
		BuilderDao dao = new BuilderDao();
		// Null Check
		for (int index = 0; index < uploads.size(); index++) {
			File destFile = new File(UploadPath + uploadsFileName.get(index));
			try {
				FileUtils.copyFile(uploads.get(index), destFile);
			} catch (IOException e) {

				e.printStackTrace();
			}
			dao.imageUpload(b64c.imageMaker(destFile.getPath()));
		}

		return SUCCESS;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<File> getUploads() {
		return uploads;
	}

	public void setUploads(List<File> uploads) {
		this.uploads = uploads;
	}
	
	public List<String> getUploadsContentType() {
		return uploadsContentType;
	}

	public void setUploadsContentType(List<String> uploadsContentType) {
		this.uploadsContentType = uploadsContentType;
	}

	public List<String> getUploadsFileName() {
		return uploadsFileName;
	}

	public void setUploadsFileName(List<String> uploadsFileName) {
		this.uploadsFileName = uploadsFileName;
	}
	


	public List<ProductVO> getImageList() {
		return imageList;
	}


	public void setImageList(List<ProductVO> imageList) {
		this.imageList = imageList;
	}


	public String getBlog_font() {
		return blog_font;
	}

	public void setBlog_font(String blog_font) {
		this.blog_font = blog_font;
	}

	public String getBlog_bgcolor() {
		return blog_bgcolor;
	}

	public void setBlog_bgcolor(String blog_bgcolor) {
		this.blog_bgcolor = blog_bgcolor;
	}

	public String getBlog_style() {
		return blog_style;
	}

	public void setBlog_style(String blog_style) {
		this.blog_style = blog_style;
	}

	public int getBlog_slider() {
		return blog_slider;
	}

	public void setBlog_slider(int blog_slider) {
		this.blog_slider = blog_slider;
	}	

	public int getBlog_map() {
		return blog_map;
	}

	public void setBlog_map(int blog_map) {
		this.blog_map = blog_map;
	}	

	public Shop_blogVO getShop_blog() {
		return shop_blog;
	}


	public void setShop_blog(Shop_blogVO shop_blog) {
		this.shop_blog = shop_blog;
	}


	public ShopVO getShop() {
		return shop;
	}


	public void setShop(ShopVO shop) {
		this.shop = shop;
	}
	
	


	public int getShop_seq() {
		return shop_seq;
	}


	public void setShop_seq(int shop_seq) {
		this.shop_seq = shop_seq;
	}
	
	


	public ArrayList<String> getSkinList() {
		return skinList;
	}


	public void setSkinList(ArrayList<String> skinList) {
		this.skinList = skinList;
	}
	
	


	public String getSkin() {
		return skin;
	}


	public void setSkin(String skin) {
		this.skin = skin;
	}	
	


	public ArrayList<String> getFontList() {
		return fontList;
	}


	public void setFontList(ArrayList<String> fontList) {
		this.fontList = fontList;
	}


	public ArrayList<String> getColorList() {
		return colorList;
	}


	public void setColorList(ArrayList<String> colorList) {
		this.colorList = colorList;
	}
	
	


	@Override
	public void setSession(Map<String, Object> arg0) {
		sessionMap=(SessionMap<String, Object>) arg0;		
	}
	
	
}
